package cn.dong.sms.controller.api;

import cn.dong.api.domain.CommonResult;
import cn.dong.sms.service.SmsService;
import cn.dong.sms.utils.SmsProperties;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/sms")
@RestController
public class SmsController {


    @Autowired
    private SmsService smsService;

    @Autowired
    private SmsProperties smsProperties;

    /**
     * 发送短信验证码
     *
     * @param phone
     * @return
     * @throws ClientException
     */
    @GetMapping("/send/{phone}")
    public CommonResult send(@PathVariable String phone) throws ClientException {
        String code = smsService.send(phone);
        return CommonResult.ok().data(code);
    }


    /**
     * 测试配置中心
     *
     * @return
     */
    @GetMapping("/testConfig")
    public String testConfig() {
        return smsProperties.getAccessId() + "\t\n" + smsProperties.getAccessSecret() + "\t\n" + smsProperties.getSignature()
                + "\t\n" + smsProperties.getSignature()
                + "\t\n" + smsProperties.getTemplate();
    }

}
